package logic

import (
	"context"

	"mall/common/enumx"
	"mall/common/modelx"
	"mall/common/mqueuex"
	"mall/service/user/model"
	"mall/service/user/rpc/internal/svc"
	"mall/service/user/rpc/types/user"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

type UserInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserInfoLogic) UserInfo(in *user.UserInfoReq) (*user.UserInfoResp, error) {
	if in.Id == 0 {
		return nil, status.Error(500, "user id is required")
	}

	res, err := l.svcCtx.User.FindOne(l.ctx, in.Id, []string{})
	if err != nil {
		return nil, err
	}

	userInfo := &user.UserInfo{
		Id:     int64(res.ID),
		Mobile: res.UserMobile,
		Email:  res.UserEmail,
		Name:   res.UserLogin,
	}
	var userAccount *user.UserAccountResp

	switch in.AuthType {
	case mqueuex.UserAuthTypeWxMini:
		uid := res.ID
		authFilterMap := make(map[string]map[string][]interface{})
		authFilterMap[model.UserAuthColumns.UserID] = map[string][]interface{}{
			"equalTo": []interface{}{uid},
		}
		authFilterMap[model.UserAuthColumns.PlatformID] = map[string][]interface{}{
			"equalTo": []interface{}{enumx.ThirdPartyPlatformWechatMini},
		}
		var pageSize int64 = 1
		authList, _, err := l.svcCtx.User.GetAuthList(l.ctx, model.M{
			"pageSize": pageSize,
		}, -1, authFilterMap) // 读取关联表
		if err != nil || len(authList) == 0 {
			return nil, err
		}

		userAuth := authList[0]
		userInfo.Nickname = userAuth.Nickname
		userInfo.Avatar = userAuth.Avatar
		userInfo.Gender = int32(userAuth.Gender)
		userInfo.Birthday = modelx.FormatNullDotTime(userAuth.Birthday, "2006-01-02")

		accountFilterMap := make(map[string]map[string][]interface{})
		accountFilterMap[model.UserAccountColumns.UserID] = map[string][]interface{}{
			"equalTo": []interface{}{uid},
		}
		accountList, _, listErr := l.svcCtx.User.GetAccountList(l.ctx, model.M{
			"pageSize": pageSize,
		}, -1, accountFilterMap)
		if listErr != nil || len(accountList) == 0 {
			return nil, listErr
		}

		account := accountList[0]
		userAccount = &user.UserAccountResp{
			Point:         int64(account.Point),
			FrozenPoint:   int64(account.FrozenPoint),
			HistoryPoint:  int64(account.HistoryPoint),
			Balance:       int64(account.Balance),
			FrozenBalance: int64(account.FrozenBalance),
			Loan:          int64(account.Loan),
			Credit:        int64(account.Credit),
			Level:         int32(account.Level),
		}
		userInfo.Level = int32(account.Level)

		metaFilterMap := make(map[string]map[string][]interface{})
		metaFilterMap[model.UserMetumColumns.UserID] = map[string][]interface{}{
			"equalTo": []interface{}{uid},
		}
		metaFilterMap[model.UserMetumColumns.MetaKey] = map[string][]interface{}{
			"equalTo": []interface{}{"_birthday_hidden"},
		}
		metaList, _, listErr := l.svcCtx.User.GetMeta(l.ctx, model.M{}, -1, metaFilterMap)
		if listErr != nil {
			return nil, listErr
		}

		if len(metaList) > 0 {
			for _, m := range metaList {
				if m.MetaKey != "" && m.MetaValue.Valid {
					if m.MetaKey == "_birthday_hidden" && m.MetaValue.String == "1" {
						userInfo.BirthdayHidden = 1
					}
				}
			}
		}
	}

	return &user.UserInfoResp{
		UserInfo:    userInfo,
		UserAccount: userAccount,
	}, nil
}
